import Base from 'models/base';

import SystemUtil from 'utils/systemUtil';

class CartGoods extends Base {
  constructor(goods, cart) {
    super();

    this.id = goods.buyCarLogId;
    this.defId = goods.goodsDefId;
    this.modelId = goods.goodsModelId;
    this.name = goods.goodsName;
    this.modelName = goods.goodsModelName;
    this.buyMethod = goods.buyMethod;
    this.buyCount = goods.buyCount;
    this.oldBuyCount = goods.buyCount;
    this.pic = this.getOnePic(goods.goodsPic);
    this.totalCount = goods.stockCount;
    this.isShow = false;
    this.cart = cart;

    let status = goods.status;
    if (status === 1) {
      this.checked = false;
    } else {
      this.checked = undefined;
    }
    this.status = status;

    let price = goods.goodsModelSalePrice;
    this.price = Number(price);
  }

  checkChange() {
    let checked = this.checked;
    let cart = this.cart;
    let cartInfo = cart.cartInfo;
    let type = this.buyMethod;
    if (checked) {
      cart.resetCheck(type);
      cartInfo.resetCheck();
    } else {
      cart.goodsObj[type].checked = false;
      cartInfo.checked = false;
    }
  }

  setCheckAll(checked) {
    if (this.checked === undefined) {
      return;
    }

    if (this.checked !== checked) {
      this.checked = checked;
    }
  }
}

class Cart extends Base {
  constructor(cart, goodsList, cartInfo) {
    super();

    this.name = cart.storeName;
    this.id = SystemUtil.getId();
    this.cartInfo = cartInfo;
    this.branchId = cart.branchId;
    this.partnerId = cart.partnerId;

    let distance = cart.distances;
    let distanceText;
    if (distance === null) {
      distance = '';
      distanceText = '';
    } else {
      distance = Number(distance);
      if (distance < 1000) {
        distance = Number(distance.toFixed(1));
        distanceText = `${distance}M`;
      } else {
        distance /= 1000;
        distance = Number(distance.toFixed(1));
        if (distance > 1000) {
          distanceText = `1000+KM`;
        } else {
          distanceText = `${distance}KM`;
        }
      }
    }
    this.distance = distance;
    this.distanceText = distanceText;

    let goodsArr = [];
    let goodsObj = {};
    for (let i = 0; i < goodsList.length; i++) {
      let goods = goodsList[i];
      goods = new CartGoods(goods, this);
      goodsArr.push(goods);

      let buyMethod = goods.buyMethod;
      let obj = goodsObj[buyMethod];
      if (obj === undefined) {
        obj = {
          checked: false,
          data: []
        };
        goodsObj[buyMethod] = obj;
      }
      obj.data.push(goods);
    }

    this.goodsList = goodsArr;
    this.goodsObj = goodsObj;
  }

  changeGoodsShow(goods, isShow) {
    let goodsList = this.goodsList;
    for (let i = 0; i < goodsList.length; i++) {
      const obj = goodsList[i];
      if (obj === goods) {
        continue;
      }

      if (obj.isShow === isShow) {
        continue;
      }
      obj.isShow = isShow;
    }
  }

  checkChange(type) {
    let goodsObj = this.goodsObj;
    let goodsData = goodsObj[type];
    if (goodsData === undefined) {
      return;
    }

    let checked = goodsData.checked;
    this.setCheckAll(type, checked);

    let cartInfo = this.cartInfo;
    if (checked) {
      cartInfo.resetCheck();
    } else {
      cartInfo.checked = false;
    }
  }

  setCheckAll(type, checked) {
    let goodsObj = this.goodsObj;
    let goodsData = goodsObj[type];
    if (goodsData === undefined) {
      return;
    }

    if (goodsData.checked !== checked) {
      goodsData.checked = checked;
    }

    let goodsList = goodsData.data;
    for (let i = 0; i < goodsList.length; i++) {
      let goods = goodsList[i];
      goods.setCheckAll(checked);
    }
  }

  resetCheck(type) {
    let goodsObj = this.goodsObj;
    let goodsData = goodsObj[type];
    if (goodsData === undefined) {
      return;
    }

    let goodsList = goodsData.data;
    for (let i = 0; i < goodsList.length; i++) {
      let goods = goodsList[i];
      if (!goods.checked) {
        goodsData.checked = false;
        return;
      }
    }
    goodsData.checked = true;
  }

  getCheckStatus() {
    let goodsObj = this.goodsObj;
    for (const key in goodsObj) {
      let obj = goodsObj[key];
      if (!obj.checked) {
        return false;
      }
    }
    return true
  }

  getShopParam() {
    let partnerId = this.partnerId;
    if (!SystemUtil.isEmpty(partnerId)) {
      return {
        partnerId
      };
    }

    return {
      branchId: this.branchId
    };
  }

  deleteGoods(goods) {
    let goodsList = this.goodsList;
    let index = goodsList.indexOf(goods);
    if (index > -1) {
      goodsList.splice(index, 1);
    }

    let type = goods.buyMethod;
    let goodsObj = this.goodsObj;
    let obj = goodsObj[type];
    if (obj !== undefined) {
      let data = obj.data;
      if (data.length <= 1) {
        delete goodsObj[type];
        this.cartInfo.resetCheck();
        this.id = SystemUtil.getId();
      } else {
        let dataIndex = data.indexOf(goods);
        if (dataIndex > -1) {
          data.splice(dataIndex, 1);
          this.resetCheck(type);
          this.cartInfo.resetCheck();
        }
      }
    }
  }

  getCheckGoods() {
    let goodsList = this.goodsList;
    let goodsData = [];
    for (let i = 0; i < goodsList.length; i++) {
      const goods = goodsList[i];
      if (!goods.checked) {
        continue;
      }
      goodsData.push({
        goodsDefId: goods.defId,
        goodsModelId: goods.modelId,
        buyCount: goods.buyCount
      });
    }
    return goodsData;
  }
}

export default class CartInfo extends Base {
  constructor(data) {
    super();

    let cartList = [];
    for (let i = 0; i < data.length; i++) {
      const cartArr = data[i];
      let cart = cartArr[0];
      cart = new Cart(cart, cartArr, this);
      cartList.push(cart);
    }
    this.cartList = cartList;

    if (cartList.length > 0) {
      this.checked = false;
    } else {
      this.checked = undefined;
    }
  }

  checkChange() {
    let checked = this.checked;
    this.setCheckAll(checked);
  }

  setCheckAll(checked) {
    if (this.checked !== checked) {
      this.checked = checked;
    }

    let cartList = this.cartList;
    for (let i = 0; i < cartList.length; i++) {
      let cart = cartList[i];
      cart.setCheckAll(1, checked);
      cart.setCheckAll(2, checked);
    }
  }

  resetCheck() {
    let cartList = this.cartList;
    for (let i = 0; i < cartList.length; i++) {
      let cart = cartList[i];
      let checked = cart.getCheckStatus();
      if (!checked) {
        this.checked = false;
        return;
      }
    }
    this.checked = true;
  }

  getStatData() {
    let cartList = this.cartList;
    let count = 0,
      totalPrice = 0;
    for (let i = 0; i < cartList.length; i++) {
      const cart = cartList[i];
      let goodsList = cart.goodsList;
      for (let j = 0; j < goodsList.length; j++) {
        const goods = goodsList[j];
        let checked = goods.checked;
        if (checked) {
          let buyCount = goods.buyCount;
          let price = goods.price;
          count += buyCount;
          totalPrice += buyCount * price;
        }
      }
    }

    return {
      count: SystemUtil.roundValue(count),
      totalPrice: SystemUtil.roundValue(totalPrice, 2)
    };
  }

  deleteGoods(goods) {
    let cart = goods.cart;
    let goodsList = cart.goodsList;
    if (goodsList.length <= 1) {
      let cartList = this.cartList;
      let index = cartList.indexOf(cart);
      cartList.splice(index, 1);
      this.resetCheck();
      if (cartList.length === 0) {
        this.checked = undefined;
      }
      return;
    }
    cart.deleteGoods(goods);
  }

  getGoodsList() {
    let cartList = this.cartList;
    let goodsList = [];
    let goodsCartList = [];
    for (let i = 0; i < cartList.length; i++) {
      const cart = cartList[i];
      let goodsData = cart.getCheckGoods();
      if (goodsData.length > 0) {
        goodsList.push(...goodsData);
        goodsCartList.push(cart);
      }
    }
    return {
      goodsList,
      goodsCartList
    };
  }
}
